package com.prs.crm.action.cdxh;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.cdxh.MaterialProduct;
import com.prs.crm.domain.cdxh.SaleOrder;
import com.prs.crm.domain.cdxh.SaleOrderDetail;
import com.prs.crm.domain.cdxh.SerialNumberss;
import com.prs.crm.domain.cdxh.material.MaterialTeam;
import com.prs.crm.domain.cdxh.printing.PrintPrintIng;
import com.prs.crm.domain.cdxh.printing.PrintPrintIngDetail;
import com.prs.crm.domain.cdxh.printing.Printing;
import com.prs.crm.domain.cdxh.printing.PrintingTeam;
import com.prs.crm.service.cdxh.MaterialProductService;
import com.prs.crm.service.cdxh.MaterialTeamService;
import com.prs.crm.service.cdxh.PrintPrintIngService;
import com.prs.crm.service.cdxh.PrintingService;
import com.prs.crm.service.cdxh.PrintingTeamService;
import com.prs.crm.service.cdxh.SaleOrderService;
import com.prs.crm.service.cdxh.SerialNumberssService;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2010-12-9 下午03:54:14 简单说明
 */

public class PrintingTeamAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private PaginationSupport pagination;

	private List<Printing> mdlist = new ArrayList<Printing>();

	private PrintPrintIng printPrintIng;

	private Collection<PrintPrintIngDetail> slist;

	private PrintPrintIngService printPrintIngService;
	//zhoupeng
	private SerialNumberssService serialNumberssService;
	
	public SerialNumberssService getSerialNumberssService() {
		return serialNumberssService;
	}

	public void setSerialNumberssService(SerialNumberssService serialNumberssService) {
		this.serialNumberssService = serialNumberssService;
	}

	private MaterialTeamService materialTeamService;
	
	public MaterialTeamService getMaterialTeamService() {
		return materialTeamService;
	}

	public void setMaterialTeamService(MaterialTeamService materialTeamService) {
		this.materialTeamService = materialTeamService;
	}

	private SaleOrderService saleOrderService;
	
	public SaleOrderService getSaleOrderService() {
		return saleOrderService;
	}

	public void setSaleOrderService(SaleOrderService saleOrderService) {
		this.saleOrderService = saleOrderService;
	}

	private MaterialProductService materialProductService;

	public MaterialProductService getMaterialProductService() {
		return materialProductService;
	}

	public void setMaterialProductService(
			MaterialProductService materialProductService) {
		this.materialProductService = materialProductService;
	}

	private Printing printing;

	private PrintingService printingService;

	private PrintingTeam printingTeam;

	private PrintingTeamService printingTeamService;

	private String presentTime = "2010年10月10日";

	private Integer[] ids;
	
	private  String idss;
	/**
	 * 明细
	 * 
	 * @return
	 */
	private String[] print_size;
	private String[] print_color;
	private String[] packway;
	private Integer[] print_order_numg;
	private Integer[] print_articles;
	private Integer[] print_shiji_num;
	private Integer[] print_feiliao;  //添加的废料
	private String[] print_fangxian;
	private String[] print_jianxian;
	private String[] print_baozhuang;
	private String[] print_customer;
	private String[] print_shijian;
	private String[] print_remark;
	private Integer[] firstYN;
	private String[] printOrderPlans;
	private String[] printCodes;
	private Date[] tihuodates;
	private String[] rushOrders;
    private String allids="";	
	private String pagetrunNum="";
	@Action("viewPrintTeam")
	public String viewPrintTeam() {
		printingTeam = this.getPrintingTeamService().get(printingTeam.getId());
		return SUCCESS;
	}

	@Action("listPrintingTeam")
	public String listPrintingTeam() {
		
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Printing.class);
		criteria.add(Restrictions.lt("printShowState", 6));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPrintingService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listPrintingTeamPrint")
	public String listPrintingTeamPrint() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PrintPrintIng.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPrintPrintIngService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewPrintingTeamPrint")
	public String viewPrintingTeamPrint() {
		printPrintIng = this.getPrintPrintIngService().get(
				printPrintIng.getId());
        slist=printPrintIng.getDetails();
		return SUCCESS;
	}

	@Action("listPrintingPrint")
	public String listPrintingPrint() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PrintingTeam.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPrintingTeamService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editPrintTeam")
	public String editPrintTeam() {
		String[] ss=pagetrunNum.split(",");
		for(int j=0;j<ss.length;j++){
			if(ss[j]!=""){				
					Printing mdObject = this.getPrintingService().get(Integer.parseInt(ss[j]));
					
					mdlist.add(mdObject);
					
					if(idss==null){
						idss=ss[j];
					}else{
						idss=idss+","+ss[j];
					}
					
				
			}
			}
		
		
		
//		System.out.println(idss+"aaaaaa");
	
	
		presentTime = getPresentTimes();
		

		return SUCCESS;
	}

	@Clew(results = { "success", "none", "error" })
	@Action(value = "savePrintProduct", results = {
			@Result(name = "success", type = "redirect", location = "listPrintingTeam"),
			@Result(name = "error", type = "redirect", location = "listPrintingTeam") })
	public String savePrintProduct() {
		
		this.getPrintingTeamService().saveALLPrintingTeam(print_feiliao,printingTeam,
				print_size, print_color, packway, print_order_numg,
				print_articles, print_shiji_num, print_fangxian,
				print_jianxian, print_baozhuang, print_customer, print_shijian,
				print_remark, firstYN, printOrderPlans, printCodes, tihuodates,
				rushOrders);
		//废料打回
		for(int i=0;i<ids.length;i++){
			Printing pt = this.getPrintingService().get(ids[i]);
			/**
			 * 新加一个生成单据
			 */
			if(print_feiliao[i]>0){
				MaterialTeam mtt = this.getMaterialTeamService().getMaterialTeam(printCodes[i],pt.getPrintOrderPlan());
				String Scal = mtt.getMaterialScal();
				MaterialProduct mpObject = new MaterialProduct();
				mpObject.setProductCode(printCodes[i]);
				mpObject.setProductName(pt.getPrintName());
				mpObject.setProductColor(print_color[i]);
				mpObject.setProductNorm(print_size[i]);
				mpObject.setDeliveryDate(new Date());
				mpObject.setProductNum(print_feiliao[i]);
				mpObject.setPlanOrOrderId(setOrderCodes(pt.getPrintOrderPlan()));
				mpObject.setProductPackstyle(packway[i]);
				mpObject.setProductScal(Scal);
				mpObject.setNeedNum(getUj(Scal, print_size[i], print_feiliao[i].toString()));
				mpObject.setRushOrder(rushOrders[i]);
				mpObject.setpLState(1);
				mpObject.setAlreadyProduct(0.000);
				mpObject.setFirstYN(1);
				mpObject.setRemainderNum(0.000);
				mpObject.setBackMakeState(3);
				mpObject.setNumConfirmState(1);
				this.getMaterialProductService().save(mpObject);
			}
		}
		this.setPrompt("该印刷单创建成功!");
		return SUCCESS;
	}
	/**
	 * 
	 * @param codes
	 * @return
	 */
	String setOrderCodes(String codes) {
		String op = "";
		SerialNumberss snObject = this.getSerialNumberssService()
				.getSerialNumberssDis(codes);
		if (snObject == null) {
			SerialNumberss snmm = new SerialNumberss();
			snmm.setStyCategory("1LH");
			snmm.setStyNumberCode(codes);
			snmm.setJudgStyle("PS");
			this.getSerialNumberssService().save(snmm);
			op = "1LH" + codes;
		} else {
			SerialNumberss snmm = new SerialNumberss();
			String zjug = addCodes(snObject.getStyCategory());
			snmm.setStyCategory(zjug);
			snmm.setStyNumberCode(codes);
			snmm.setJudgStyle("PS");
			this.getSerialNumberssService().save(snmm);
			op = zjug + codes;
		}

		return op;

	}
	String addCodes(String dd) {
		String d[] = dd.split("L");
		int s = Integer.parseInt(d[0]) + 1;
		String ds = s + "LH";
		return ds;
	}
	double getUj(String saqus, String norm, String nums) {
		String ss[] = norm.split("\\*");
		double rU = (Double.parseDouble(saqus) * Double.parseDouble(ss[0])
				* Double.parseDouble(ss[1]) * Double.parseDouble(nums)) / 1000;
		return rU;

	}
	@Clew(results = { "success", "none", "error" })
	@Action(value = "savePrintTeamPrint", results = {
			@Result(name = "success", type = "redirect", location = "listPrintingTeam"),
			@Result(name = "error", type = "redirect", location = "listPrintingTeam") })
	public String savePrintTeamPrint() {

		this.getPrintingTeamService().saveALLPrintingTeamPrint(printingTeam,
				print_size, print_color, packway, print_order_numg,
				print_articles, print_shiji_num, print_fangxian,
				print_jianxian, print_baozhuang, print_customer, print_shijian,
				print_remark, firstYN, printOrderPlans, printCodes, tihuodates,
				rushOrders);
		this.setPrompt("该印刷单创建打印预览成功!");
		return SUCCESS;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PrintingTeam getPrintingTeam() {
		return printingTeam;
	}

	public void setPrintingTeam(PrintingTeam printingTeam) {
		this.printingTeam = printingTeam;
	}

	public PrintingTeamService getPrintingTeamService() {
		return printingTeamService;
	}

	public void setPrintingTeamService(PrintingTeamService printingTeamService) {
		this.printingTeamService = printingTeamService;
	}

	public Printing getPrinting() {
		return printing;
	}

	public void setPrinting(Printing printing) {
		this.printing = printing;
	}

	public PrintingService getPrintingService() {
		return printingService;
	}

	public void setPrintingService(PrintingService printingService) {
		this.printingService = printingService;
	}

	String getPresentTimes() {
		int yyyy, MM, dd;
		Calendar c = Calendar.getInstance();
		yyyy = c.get(Calendar.YEAR);
		MM = c.get(Calendar.MONTH) + 1;
		dd = c.get(Calendar.DAY_OF_MONTH);
		String Times = yyyy + "年" + MM + "月" + dd + "日";
		return Times;
	}

	public String getPresentTime() {
		return presentTime;
	}

	public void setPresentTime(String presentTime) {
		this.presentTime = presentTime;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public List<Printing> getMdlist() {
		return mdlist;
	}

	public void setMdlist(List<Printing> mdlist) {
		this.mdlist = mdlist;
	}

	public String[] getPrint_size() {
		return print_size;
	}

	public void setPrint_size(String[] print_size) {
		this.print_size = print_size;
	}

	public String[] getPrint_color() {
		return print_color;
	}

	public void setPrint_color(String[] print_color) {
		this.print_color = print_color;
	}

	public String[] getPackway() {
		return packway;
	}

	public void setPackway(String[] packway) {
		this.packway = packway;
	}

	public Integer[] getPrint_order_numg() {
		return print_order_numg;
	}

	public void setPrint_order_numg(Integer[] print_order_numg) {
		this.print_order_numg = print_order_numg;
	}

	public Integer[] getPrint_articles() {
		return print_articles;
	}

	public void setPrint_articles(Integer[] print_articles) {
		this.print_articles = print_articles;
	}

	public Integer[] getPrint_shiji_num() {
		return print_shiji_num;
	}

	public void setPrint_shiji_num(Integer[] print_shiji_num) {
		this.print_shiji_num = print_shiji_num;
	}

	public String[] getPrint_fangxian() {
		return print_fangxian;
	}

	public void setPrint_fangxian(String[] print_fangxian) {
		this.print_fangxian = print_fangxian;
	}

	public String[] getPrint_jianxian() {
		return print_jianxian;
	}

	public void setPrint_jianxian(String[] print_jianxian) {
		this.print_jianxian = print_jianxian;
	}

	public String[] getPrint_baozhuang() {
		return print_baozhuang;
	}

	public void setPrint_baozhuang(String[] print_baozhuang) {
		this.print_baozhuang = print_baozhuang;
	}

	public String[] getPrint_customer() {
		return print_customer;
	}

	public void setPrint_customer(String[] print_customer) {
		this.print_customer = print_customer;
	}

	public String[] getPrint_shijian() {
		return print_shijian;
	}

	public void setPrint_shijian(String[] print_shijian) {
		this.print_shijian = print_shijian;
	}

	public String[] getPrint_remark() {
		return print_remark;
	}

	public void setPrint_remark(String[] print_remark) {
		this.print_remark = print_remark;
	}

	public Integer[] getFirstYN() {
		return firstYN;
	}

	public void setFirstYN(Integer[] firstYN) {
		this.firstYN = firstYN;
	}

	public String[] getPrintOrderPlans() {
		return printOrderPlans;
	}

	public void setPrintOrderPlans(String[] printOrderPlans) {
		this.printOrderPlans = printOrderPlans;
	}

	public String[] getPrintCodes() {
		return printCodes;
	}

	public void setPrintCodes(String[] printCodes) {
		this.printCodes = printCodes;
	}

	public Date[] getTihuodates() {
		return tihuodates;
	}

	public void setTihuodates(Date[] tihuodates) {
		this.tihuodates = tihuodates;
	}

	public String[] getRushOrders() {
		return rushOrders;
	}

	public void setRushOrders(String[] rushOrders) {
		this.rushOrders = rushOrders;
	}

	public PrintPrintIng getPrintPrintIng() {
		return printPrintIng;
	}

	public void setPrintPrintIng(PrintPrintIng printPrintIng) {
		this.printPrintIng = printPrintIng;
	}

	public PrintPrintIngService getPrintPrintIngService() {
		return printPrintIngService;
	}

	public void setPrintPrintIngService(
			PrintPrintIngService printPrintIngService) {
		this.printPrintIngService = printPrintIngService;
	}

	public Collection<PrintPrintIngDetail> getSlist() {
		return slist;
	}

	public void setSlist(Collection<PrintPrintIngDetail> slist) {
		this.slist = slist;
	}

	public String getIdss() {
		return idss;
	}

	public void setIdss(String idss) {
		this.idss = idss;
	}

	public String getAllids() {
		return allids;
	}

	public void setAllids(String allids) {
		this.allids = allids;
	}

	public String getPagetrunNum() {
		return pagetrunNum;
	}

	public void setPagetrunNum(String pagetrunNum) {
		this.pagetrunNum = pagetrunNum;
	}
	public Integer[] getPrint_feiliao() {
		return print_feiliao;
	}

	public void setPrint_feiliao(Integer[] printFeiliao) {
		print_feiliao = printFeiliao;
	}
}
